package com.cdream.basic.algorithm.leetcode.slideWindow;

/**
 * 滑动窗口+频率数组法
 * @author cdream
 * @date 5/20/21
 */
public class Question3 {
    public static void main(String[] args) {
        System.out.println(new Question3().lengthOfLongestSubstring(" "));
    }

    public int lengthOfLongestSubstring(String s) {
        int[] freq = new int[256];

        int start = 0;
        int end = 0;
        int min = 0;
        while (end <= s.length() - 1) {
            // 右侧不存在，右边界向右移动
            if (freq[s.charAt(end)] == 0) {
                freq[s.charAt(end++)]++;
            } else {
                freq[s.charAt(start++)]--;
            }
            min = Math.max(end - start, min);
        }

        return min;
    }
}
